package com.edisoncgh.controller;

import com.edisoncgh.pojo.ArtTag;
import com.edisoncgh.pojo.Article;
import com.edisoncgh.pojo.CommentInfo;
import com.edisoncgh.service.ArtTagService;
import com.edisoncgh.service.CommentsService;
import com.edisoncgh.service.TagService;
import com.edisoncgh.service.impl.ArticleServiceImpl;
import com.edisoncgh.service.impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/articleOption")
public class ArticleController {
    @Autowired
    private ArticleServiceImpl articleService;
    @Autowired
    private CategoryServiceImpl categoryService;
    @Autowired
    private ArtTagService artTagService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CommentsService commentsService;

    @RequestMapping("/allArticle")
    public String allArticle(Model model){
        List<Article> list=articleService.findall();
        model.addAttribute("list",list);
        for(Article article : list) {
            String category = categoryService.selectById(article.getArt_category()).getCname();
            article.setCategory_name(category);
            String tag = tagService.selectById(artTagService.selectByArticleId(article.getId()).getTag_id()).getTag_name();
            article.setTag_name(tag);
        }
        return "article";
    }

    @RequestMapping("/deleteArticle")
    public String deleteArticle(Integer art_id) {
        articleService.deleteById(art_id);
        return "redirect:/articleOption/allArticle";
    }

    @RequestMapping("/addArticle")
    public String addArticle(HttpServletRequest request, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF8");
        String title = request.getParameter("article_title");
        String article_content = request.getParameter("article_content");
        String art_category = request.getParameter("article_category");
        String article_tag = request.getParameter("article_tag");
//        调试信息
        System.out.println("title:" + title + "\n" + "article_content:" + article_content + "\n" + "art_category:" + art_category + "\n" + "article_tag:" + article_tag);
//        添加到数据库

        Article article = new Article();
//        写死了
        article.setTitle(title);
        article.setContent(article_content);
        article.setArt_category(1);
        article.setAuthor("sifour");
//        article.setTitle(title.toString());
//        写死了
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //获取当前时间String类型
        String time_string = sdf.format(date);
        Date time_date = null;
            try {
                time_date = sdf.parse(sdf.format(date));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        article.setPublish_date(time_date);
        article.setVisited_times(0);
        article.setArt_category(categoryService.selectByName(art_category));

//        插入ArtTag表


//        Exception
        if (articleService.insertArticle(article)){
            System.out.println("发表文章成功");
        }else {
            System.out.println("发表文章失败");
        }

        return "redirect:/articleOption/allArticle";
    }

    @RequestMapping("/indexArticle")
    public String indexArticle(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        List<Article> list=articleService.findall();
        model.addAttribute("indexArticleList",list);
        for(Article article : list) {
            System.out.println("title:" + article.getTitle());
        }
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("index");
        return "index";
    }

    @RequestMapping("articleDetail")
    public String articleDetail(Integer art_id, HttpServletRequest request,Model model) {
        Article article = articleService.selectById(art_id);
        article.setCategory_name(categoryService.selectById(article.getArt_category()).getCname());
        article.setTag_name(tagService.selectById(artTagService.selectByArticleId(article.getId()).getTag_id()).getTag_name());
        request.getSession().setAttribute("art_title", article.getTitle());
        request.getSession().setAttribute("publish_time", article.getPublish_date());
        request.getSession().setAttribute("art_cate", article.getCategory_name());
        request.getSession().setAttribute("art_tag", article.getTag_name());
        request.getSession().setAttribute("art_content", article.getContent());
        request.getSession().setAttribute("comment_art_id", art_id);

        List<CommentInfo> list = commentsService.findByArtId(art_id);
        model.addAttribute("list",list);
//        调试
        System.out.println(article);

        return "articledetail";

    }
    @RequestMapping("/modifyArticle")
    public String modifyArticle(){

        return "article";
    }

}
